#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=2005;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
    return x*f;
}
int wtp,wstk[40];
void write(int x,char c){
    if(x<0) x=-x,putchar('-');
    do{wstk[++wtp]=x%10,x/=10;}while(x);
    while(wtp){putchar(wstk[wtp--]^48);}
    if(c) putchar(c);
}
int n,ans,a[N],dp[N][N];
void solve(){
    n=read(),ans=0;
    for(int i=1;i<=n;i++) a[i]=read();
    for(int i=1;i<=n;i++) for(int j=0;j<i;j++) dp[i][j]=0;
    for(int i=1;i<n;i++) for(int j=0;j<i;j++){
        dp[i+1][j]=max(dp[i+1][j],dp[i][j]+(a[i]==a[i+1])*a[i+1]);
        dp[i+1][i]=max(dp[i+1][i],dp[i][j]+(a[j]==a[i+1])*a[i+1]);
    }
    for(int i=0;i<n;i++) ans=max(ans,dp[n][i]);
    write(ans,'\n');
}
signed main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    for(int T=read();T;T--) solve();
    return 0;
}
